//
//  CJExten+Attr.swift
//  CJCore
//
//  Created by 陈警卫 on 2024-06-05.
//

import UIKit
import Foundation

public extension NSString {
    
    /// String 转 NSMutableAttributedString
    @objc func cj_attr(color: UIColor? = nil, font: UIFont? = nil) -> NSMutableAttributedString {
        let value = self as String
        let attr = value.cj_attr(color: color, font: font)
        return attr
    }
}

public extension String {
    
    /// String 转 NSMutableAttributedString
    func cj_attr(color: UIColor? = nil, font: UIFont? = nil) -> NSMutableAttributedString {
        let arrt = NSMutableAttributedString(string: self)
        if let color = color {
            arrt.setAttributes([.foregroundColor: color], range: NSMakeRange(0, arrt.length))
        }
        if let font = font {
            arrt.setAttributes([.font: font], range: NSMakeRange(0, arrt.length))
        }
        return arrt
    }
}

public extension NSMutableAttributedString {
    
    /// 设置颜色
    @objc func cj_color_oc(color: UIColor, range: NSRange) -> NSMutableAttributedString {
        return cj_color(color: color, range: range)
    }
    /// 设置颜色
    func cj_color(color: UIColor, range: NSRange? = nil) -> NSMutableAttributedString {
        var range = range
        if range == nil {
            range = NSMakeRange(0, self.length)
        }
        self.setAttributes([.foregroundColor: color], range: range!)
        return self
    }
    
    /// 设置字体
    @objc func cj_font_oc(font: UIFont, range: NSRange) -> NSMutableAttributedString {
        return cj_font(font: font, range: range)
    }
    /// 设置字体
    func cj_font(font: UIFont, range: NSRange? = nil) -> NSMutableAttributedString {
        var range = range
        if range == nil {
            range = NSMakeRange(0, self.length)
        }
        self.setAttributes([.font: font], range: range!)
        return self
    }
    
    /// 设置行间距
    @objc func cj_lineSpacing(value: CGFloat, range: NSRange) -> NSMutableAttributedString {
        return cj_lineSpacing(value: value, range: range)
    }
    /// 设置行间距
    func cj_lineSpacing(value: CGFloat?, range: NSRange? = nil) -> NSMutableAttributedString {
        let style = NSMutableParagraphStyle()
        if let value = value {
            style.lineSpacing = value
        }
        var range = range
        if range == nil {
            range = NSMakeRange(0, self.length)
        }
        self.setAttributes([.paragraphStyle: style], range: range!)
        return self
    }
    
    /// 设置多个属性
    @objc func cj_attributes(text: String, attrs: [NSAttributedString.Key : Any]) -> NSMutableAttributedString {
        let ranges = cj_ranges(text: text)
        if !ranges.isEmpty {
            for range in ranges {
                self.setAttributes(attrs, range: range)
            }
        }
        return self
    }
    
}

public extension NSMutableAttributedString {
    
    /// 获取子字符串的range数组
    @objc func cj_ranges(text: String) -> [NSRange] {
        
        var ranges: [Range<String.Index>] = []
        var startIndex = self.string.startIndex
        
        while let range = self.string.range(of: text, range: startIndex..<self.string.endIndex) {
            ranges.append(range)
            startIndex = range.upperBound
        }
        
        var nsRanges: [NSRange] = []
        for range in ranges {
            let nsRange = NSRange(range, in: self.string)
            nsRanges.append(nsRange)
        }
        
        return nsRanges
    }
}
